package com.app.piemoment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.app.piemoment.config.Config;
import com.app.piemoment.entity.OppositeInfo;
import com.app.piemoment.image.ImageLoaderManager;
import com.app.piemoment.util.Utils;


public class EditActivity extends BaseActivity implements OnClickListener{

	private LinearLayout btn_peronal_info;
	private LinearLayout btn_expect_info;
	private ScrollView parentView;
	private LayoutInflater listInflater;
	private ImageView btn_add_image;
	private LinearLayout gallery;
	
	public EditActivity() {
		mainViewId = R.layout.edit;
	}
	
	private void addChildView(int layoutId) {
		View childView = listInflater.inflate(layoutId, null);
		parentView.removeAllViews();
		parentView.addView(childView);
	}

	

	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_left:
			super.onBackPressed();
			break;

		case R.id.btn_right:
			//TODO 保存信息
			break;
		case R.id.btn_peronal_info:
			btn_peronal_info.setBackgroundColor(getResources().getColor(R.color.bg_pink));
			((TextView)btn_peronal_info.getChildAt(0)).setTextColor(getResources().getColor(R.color.white));
			btn_expect_info.setBackgroundColor(getResources().getColor(R.color.white));
			((TextView)btn_expect_info.getChildAt(0)).setTextColor(getResources().getColor(R.color.bg_pink));
			addChildView(R.layout.edit_personal);
			break;
		case R.id.btn_expect_info:
			btn_peronal_info.setBackgroundColor(getResources().getColor(R.color.white));
			((TextView)btn_peronal_info.getChildAt(0)).setTextColor(getResources().getColor(R.color.bg_pink));
			btn_expect_info.setBackgroundColor(getResources().getColor(R.color.bg_pink));
			((TextView)btn_expect_info.getChildAt(0)).setTextColor(getResources().getColor(R.color.white));
			addChildView(R.layout.edit_want);
			break;
		case R.id.btn_add_image:
			
			break;
		default:
			break;
		}
	}


	@Override
	protected void initMainView() {
		initTitle(R.drawable.btn_back_bg,R.string.edit_title, R.drawable.btn_edit_bg,R.string.btn_confirm);
		
		listInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		btn_add_image = (ImageView)findViewById(R.id.btn_add_image);
		gallery = (LinearLayout)findViewById(R.id.gallery);
		btn_peronal_info = (LinearLayout)findViewById(R.id.btn_peronal_info);
		btn_expect_info = (LinearLayout)findViewById(R.id.btn_expect_info);
		parentView = (ScrollView)findViewById(R.id.scrollView1);
		addChildView(R.layout.edit_personal);

	}


	@Override
	protected void registerMainViewEvent() {
		btn_peronal_info.setOnClickListener(this);
		btn_expect_info.setOnClickListener(this);
		btn_add_image.setOnClickListener(this);
	}


	@Override
	protected void processMainView() {
		//TODO 个人信息更新
		processGallery();
		
	}
	
	private void processGallery() {//TODO 个人信息补充
		LinearLayout.LayoutParams imageLP = new LinearLayout.LayoutParams(Utils.dipToPixels(this, 80),Utils.dipToPixels(this, 80));
		ImageView image = null;
		gallery.removeAllViews();
		ImageLoaderManager loader = new ImageLoaderManager(this, new Handler());
		for(int i=0;i<5;i++) {
			image = new ImageView(this);
			image.setImageBitmap(loader.getImage(Config.IMAGE_URL+"", R.drawable.default_opp_icon, 0));
			gallery.addView(image,imageLP);
			image.setOnLongClickListener(new OnLongClickListener() {
				
				@Override
				public boolean onLongClick(View v) {
					new AlertDialog.Builder(EditActivity.this)
					.setItems(new String[]{"设为头像","删除"}, new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							switch (which) {
							case 0:
								Log.e("click", "设为头像");
								break;
							case 1:
								Log.e("click", "删除");
								break;

							default:
								break;
							}
						}
					}).show();
					return false;
				}
			});
		}
	}

	private void updateInfo(OppositeInfo target_info) {
		
		
	}
}
